import { Component, OnInit, Input } from '@angular/core'

import { ActivatedRoute, ParamMap } from '@angular/router'
import { Location } from '@angular/common'

import 'rxjs/add/operator/switchMap';

import { File } from './../../models/file'
import { FileService } from './../../services/file.service'

@Component({
  selector: 'jq-file-detail',
  templateUrl: './file-detail.component.html'
})
export class FileDetailComponent implements OnInit {

  constructor(
    private fileService: FileService,
    private location: Location,
    private route: ActivatedRoute
  ) {}

  ngOnInit(): void {
    this.onGetFileInfo();
  }

  onGetFileInfo(): void {
    this.route.paramMap
      // .switchMap((params: ParamMap) => this.fileService.getFile(+params.get('id')))
      .switchMap((params: ParamMap) => this.fileService.getFileSlowly(+params.get('id')))
      .subscribe(file => this.file = file);
  }

  onBack(): void {
    this.location.back();
  }

  @Input() file: File;
}
